//
//  XYStatusContentLabel.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 16/1/10.
//  Copyright © 2016年 cn.weibo. All rights reserved.
//

import UIKit

class XYStatusContentLabel: UIView {

    
    private var selectedViews: [UIView] = [UIView]()
    
    var attributeString: NSAttributedString? {
        
        didSet{
            
            textView.attributedText = attributeString
        }
    }
    
    var font: UIFont! {
        didSet{
            textView.font = font
        }
    }
    // 特殊字符的范围
    var matchResults: [XYMatchResult]? {
        
        didSet{
            print(matchResults?.count)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    private func setUpUI() {
        
        addSubview(textView)
        
        textView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var textView: UITextView = {
        
        let textView = UITextView()
        textView.backgroundColor = UIColor.clearColor() 
        textView.scrollEnabled = false
        textView.userInteractionEnabled = false
        textView.editable = false
        textView.textContainerInset = UIEdgeInsetsMake(0, -5, 0, -5)
        return textView
    }()
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        guard let matchRes = matchResults else{
            return
        }
        
        // 获取手指位置
        // 怎么判断手指的位置是否在特殊字符的范围内
        // 我们能够拿到的是手指点击的位置：CGPoint
        // 特殊字符的范围：NSRange
        let location = touches.first!.locationInView(self)
        
        let textRange = textView.characterRangeAtPoint(location)
        
        textView.selectedTextRange = textRange
        // 转化得到NSRange类型
        let range = textView.selectedRange
        
        for result in matchRes {
            
            // 判断当前点是否在特殊字符区域内
            if NSLocationInRange(range.location, result.captureRange) {
                
                textView.selectedRange = result.captureRange
                // 获取选中范围的矩形框
                let rects = textView.selectionRectsForRange(textView.selectedTextRange!)
                
                for value in rects {
                    let selectionRect = value as! UITextSelectionRect
                    let view = UIView(frame: selectionRect.rect)
                    view.layer.cornerRadius = 5
                    view.layer.masksToBounds = true
                    view.backgroundColor = UIColor.redColor()
                    insertSubview(view, atIndex: 0)
                    selectedViews.append(view)
                }
                
            }
        }
        
    }
    
    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
        for value in selectedViews {
            value.removeFromSuperview()
        }
        selectedViews.removeAll()
    }
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        touchesCancelled(touches, withEvent: event)
    }
    

}
